package com.jx.web;

import com.jx.common.CustomException;
import com.jx.common.Result;
import com.jx.pojo.User;
import com.jx.service.UserService;
import com.jx.util.TokenUtil;
import com.jx.util.ValidateCodeUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 生成验证码
     *
     * @return
     */
    @GetMapping("/sendCode")
    public Result<String> getCode(String phone, HttpServletRequest req) {
        Result<String> res = new Result<>();
        // 判断手机号是否为空
        if (!Strings.isNotEmpty(phone)) {
           throw new CustomException("手机号不能为空!");
        }
        // 发送验证码
        String code = ValidateCodeUtils.generateValidateCode(6).toString();
        // 将验证码保存到当前上下文中
        req.getServletContext().setAttribute(phone,code);
        // 设置结果集
        res.setCode(200);
        res.setMessage("成功");
        res.setData(Arrays.asList(code));
        return res;
    }

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @PostMapping("/register")
    public Result<String> userRegister(@RequestBody User user, HttpServletRequest req) {
        Result<String> res = new Result<>();
        // 判断手机号密码是否为空
        if (!Strings.isNotEmpty(user.getPhone()) || !Strings.isNotEmpty(user.getPassword())) {
            throw new CustomException("手机号或密码为空");
        }
        // 来自前端的验证码
        String phone = user.getPhone();
        String code = user.getCode();
        //后端生成的验证码
        Object codeInSession = req.getServletContext().getAttribute(phone);
        // 从当前上下文中获取生成的验证码然后与前端传递的验证码进行比较
        if (codeInSession == null || !codeInSession.equals(code)) {
            throw new CustomException("验证码错误");
        }
        // 注册用户
        userService.userRegister(user);
        // 返回结果集
        res.setCode(200);
        res.setMessage("注册成功");
        return res;
    }

    /**
     * 用户登录
     *
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result<User> userLogin(@RequestBody User user) {
        Result<User> res = new Result<>();
        // 判断手机号密码是否为空
        if (!Strings.isNotEmpty(user.getPhone()) || !Strings.isNotEmpty(user.getPassword())) {
          throw new CustomException("手机号或密码为空");
        }
        // 用户登录
        User user1 = userService.userLogin(user);
        // 设置结果集
        res.setCode(200);
        res.setMessage("成功");
        res.setData(Arrays.asList(user1));
        return res;
    }

    /**
     * 根据token查询用户信息
     * @param token
     * @return
     */
    @GetMapping("/getUserInfo")
    public Result<User> getUserInfoByToken(String token) {
        System.out.println(token);
        Result<User> res = new Result<>();
        User userInfo = userService.getUserInfoByToken(token);
        res.setCode(200);
        res.setMessage("成功");
        res.setData(Arrays.asList(userInfo));
        return res;
    }
}
